package com.knowledgePointsByMyTest.反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author yulongtian
 * @create 2024-12-17 21:44
 */
public class ReflectionDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Writer writer = new Writer();
        writer.setName("田雨龙");
        System.out.println(writer.getName());

        Class clazz = Class.forName("com.knowledgePointsByMyTest.反射.Writer");
        Constructor constructor = clazz.getConstructor();
        Object object = constructor.newInstance();
        Method setNameMethod = clazz.getMethod("setName", String.class);
        setNameMethod.invoke(object, "abc");
        Method getNameMethod = clazz.getMethod("getName");
        System.out.println(getNameMethod.invoke(object));
    }
}
